package com.doubleview.demo


/**
 * @author huchengchao <huchengchao@kuaishou.com>
 * Created on 2021-11-20
 */
def process = "ls -l".execute()
println "Found text ${process.text}"

process = "ls -l".execute()
//遍历命令进程的输入流
process.in.eachLine { line ->
    //打印每一行输入流
    println line
}


//这些是一个pipeTo命令（映射到|以允许重载），它使一个进程的输出流被传送到另一个进程的输入流。
proc1 = 'ls'.execute()
proc2 = 'tr -d o'.execute()
proc3 = 'tr -d e'.execute()
proc4 = 'tr -d i'.execute()
proc1 | proc2 | proc3 | proc4
proc4.waitFor()
if (proc4.exitValue()) {
    println proc4.err.text
} else {
    println proc4.text
}


sout = new StringBuilder()
serr = new StringBuilder()
proc2 = 'tr -d o'.execute()
proc3 = 'tr -d e'.execute()
proc4 = 'tr -d i'.execute()
proc4.consumeProcessOutput(sout, serr)
proc2 | proc3 | proc4
[proc2, proc3].each { it.consumeProcessErrorStream(serr) }
proc2.withWriter { writer ->
    writer << 'testfile.groovy'
}
proc4.waitForOrKill(1000)
println "Standard output: $sout"
println "Standard error: $serr"